package com.open.capacity.notice.provider.wechat.chain.command;

import java.util.Map;
import java.util.function.Consumer;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.springframework.stereotype.Component;

import com.open.capacity.notice.provider.wechat.chain.context.WechatMessageContext;
import com.open.capacity.notice.provider.wechat.interactive.req.SendFileRequest;
import com.open.capacity.notice.provider.wechat.interactive.res.SendFileResponse;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;

/**
 * 
 * 文件类型
 */
@Component
@RequiredArgsConstructor
public class SendWechatFileCommand implements Command {

	@Override
	public boolean execute(Context context) throws Exception {
		WechatMessageContext messageContext = (WechatMessageContext) context;
		if (messageContext.getReq() instanceof SendFileRequest) {
			SendFileRequest request = (SendFileRequest) messageContext.getReq();
			Consumer<SendFileResponse> smsResponseConsumer = smsResponse -> {
				Map<String, Object> res = BeanUtil.beanToMap(smsResponse, false, true);
				res.put("flag", smsResponse.isSuccess());
				messageContext.getSuccess().accept(res);
			};
			messageContext.getWechatTemplate().asyncOpsForChannel().sendFile(request, smsResponseConsumer,
					messageContext.getFailure());
			return true;

		}
		return false;
	}

}
